﻿Imports System.Data.SqlClient
Imports System.Data.Odbc

Public Class frmNuevaEmpresa

    Private Sub frmNuevaEmpresa_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmEmpresas.CargaEmpresas()

    End Sub

    Private Sub frmNuevaEmpresa_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaAdminPAQ()
        CargaContabilidad()
        CargaNominas()

        txtServer.Text = Server
        txtEmpresa.Clear()
        txtBD.Clear()
        txtRespaldo.Clear()
        txtEmpresa.Focus()


    End Sub
    Sub CargaAdminPAQ()
        Dim cnConn As New OdbcConnection
        cnConn.ConnectionString = "Driver={Microsoft Visual FoxPro Driver};sourcedb=\\" & Server & "\CONTPAQ\CompacW\Empresas;sourcetype=DBF;exclusive=No;"

        Dim strSql As String = "Select cNombreE01 from MGW00001 Order by cNombreE01"



        Dim cmdBuscar As New OdbcCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As OdbcDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstAdminPAQ.Items.Add(rdBuscar("cNombreE01").ToString.Trim)
        Loop
    End Sub
    Sub CargaNominas()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = "Server=" & Server & "\COMPAC" & ";database=nomGenerales;user id = SA; password = Compac123;"

        Dim strSql As String = "Select NombreEmpresa from NOM10000 Order by NombreCorto"



        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstNominas.Items.Add(rdBuscar("NombreEmpresa").ToString.Trim)
        Loop
    End Sub
    Sub CargaContabilidad()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = "Server=" & Server & "\COMPAC" & ";database=GeneralesSQL;user id = SA; password = Compac123;"

        Dim strSql As String = "Select Nombre from ListaEmpresas Order by Nombre"



        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstContabilidad.Items.Add(rdBuscar("Nombre").ToString.Trim)
        Loop
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click
        If txtEmpresa.Text = "" Then
            MsgBox("Debes indicar un nombre para la empresa", MsgBoxStyle.Exclamation, "Aviso")
            txtEmpresa.Focus()
            Exit Sub
        End If

        If txtBD.Text = "" Then
            MsgBox("Debes indicar un nombre para la base de datos", MsgBoxStyle.Exclamation, "Aviso")
            txtBD.Focus()
            Exit Sub
        End If
        'Crea la base de datos
        CreaBase()

        'CrearTABLAS ******PENDIENTE*************

        'Guarda BD
        Dim Contabilidad As String = BuscarStrDatoInicialContabilidad("SELECT AliasBDD FROM ListaEmpresas WHERE Nombre='" & lstContabilidad.Text & "'")
        Dim AdminPAQ As String = BuscarStrDatoInicialAdminPAQ("SELECT cRutaDatos FROM MGW00001 WHERE cNombreE01='" & lstAdminPAQ.Text & "'")
        Dim Nominas As String = BuscarStrDatoInicialNominas("SELECT RutaEmpresa FROM NOM10000 WHERE NombreCorto='" & lstNominas.Text & "'")
        Dim Empresa As String = txtEmpresa.Text
        Dim BD As String = txtBD.Text
        Dim Emp As New clsUsuarioPrincipal
        Emp.IdEmpresa = Emp.consecutivoEmpresas
        Emp.NombreEmpresa = Empresa
        Emp.BD = BD
        Emp.Contabilidad = Contabilidad
        Emp.Nominas = Nominas
        Emp.AdminPAQ = AdminPAQ
        Emp.Respaldo = txtRespaldo.Text
        Emp.InsertarEmpresa()
        System.IO.Directory.CreateDirectory(txtRespaldo.Text)
        Close()

    End Sub
    Sub CreaBase()
        ' La conexión a usar, indicando la base master
        Dim cnn As New SqlConnection

        Try
            ' Abrimos la conexión y ejecutamos el comando
            cnn.ConnectionString = "Server=" & Server & "\COMPAC" & ";database=master;user id = SA; password = Compac123;"
            cnn.Open()

            Dim cmd As New SqlCommand
            cmd.CommandText = "CREATE DATABASE " & txtBD.Text.Trim
            cmd.Connection = cnn

            cmd.ExecuteNonQuery()

        Catch ex As SqlException
            MsgBox("Error al crear la base de datos", MsgBoxStyle.Critical, "Aviso")
            MessageBox.Show(ex.Message, _
                            "Error al crear la base", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try
    End Sub
End Class